package com.jiehoo.utils.io;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

public class FileReader {
	public static Object readObject(String filename) {
		return readObject(new File(filename));
	}

	public static Object readObject(File file) {
		try {
			return readObject(file.toURL());
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static Object readObject(URL url) {
		ObjectInputStream input = null;
		try {
			input = new ObjectInputStream(url.openStream());
			return input.readObject();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (input != null) {
				try {
					input.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}

	public static String getFileContent(File file) {
		BufferedReader reader = null;
		StringBuffer content = new StringBuffer(1024);
		try {
			reader = new BufferedReader(new java.io.FileReader(file));
			String line = reader.readLine();
			while (line != null) {
				content.append(line);
				line = reader.readLine();
			}
			return content.toString();
		} catch (IOException e) {
			System.err.println(e.getMessage());
			return "";
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException ioe) {
					System.err.println(ioe.getMessage());
					return content.toString();
				}
			}
		}
	}

}
